﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.ExpedientesView;
using System.Collections.Generic;
using MS.InformationManagement.DAL.RecordCenter;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Linq;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;

namespace MS.InformationManagement.WP.DocumentCenter.ExpedientesList
{
    public partial class ExpedientesListUserControl : BaseControl, IExpedientesListView
    {

        #region fields

        ExpedientesListPresenter _presenter;

        #endregion

        #region Properties

        public IEnumerable<Expediente> Expedientes { get; set; }

        public int SelectedSubSerie
        {
            get { return int.Parse(ddlSubserie.SelectedValue); }
        }

        public int SelectedDocumentType
        {
            get { return int.Parse(ddlTipoDocumental.SelectedValue); }
        }

        public string SelectedRadicado
        {
            get { return txtRadicado.Text.Trim(); }
        }

        public DateTime? InitialDate
        {
            get { return dtInicio.IsDateEmpty ? new Nullable<DateTime>() : dtInicio.SelectedDate; }
        }

        public DateTime? EndDate
        {
            get { return dtFechaFinal.IsDateEmpty ? new Nullable<DateTime>() : dtFechaFinal.SelectedDate; }
        }

        public IEnumerable<Serie> Series { get; set; }

        public IEnumerable<Subserie> Subseries { get; set; }

        public IEnumerable<TipoDocumental> TiposDocumentales { get; set; }

        public int SelectedSerie
        {
            get { return int.Parse(ddlSerie.SelectedValue); }
        }

        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }

        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion

        #region form events

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void gvExpediente_databound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType != DataControlRowType.DataRow) return;
                Expediente exp = e.Row.DataItem as Expediente;
                HyperLink link = e.Row.FindControl("HyperLink1") as HyperLink;
                link.NavigateUrl = string.Format("/sites/docs/_layouts/MS.InformationManagement.WP/ExpedienteViewer.aspx?ExpedienteId={0}", exp.NúmeroExpediente);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            } 
        }

        protected void ddlSerie_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _presenter.HandleGetSubseries();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        { 
            try
            {
                if (!dtInicio.IsValid || !dtFechaFinal.IsValid) return;

                if(!dtInicio.IsDateEmpty && !dtFechaFinal.IsDateEmpty )
                    if (dtInicio.SelectedDate > dtFechaFinal.SelectedDate)
                    {
                        HandleWarning("La fecha inicial no puede ser mayor a la fecha final");
                        return;
                    }

                PageIndex = 0;
                _presenter.HandleSearch();
                pgrTemplates.BuildPager();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.GoCurrentUserHome();
        }

        protected void gvExpedientes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //gvSenders.PageIndex = e.NewPageIndex;
                //_presenter.LoadSenders();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            btnAdd.AddLoader();
            ddlSerie.AddLoader();
            _presenter = new ExpedientesListPresenter(this);
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            //pgrTemplates.NextLinkText = ">>";
            //pgrTemplates.PreviousLinkText = "<<";
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            _presenter.HandleSearch();
            pgrTemplates.BuildPager();
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            try
            {
                ddlSerie.DataSource = this.Series;
                ddlSerie.DataBind();
                ddlSerie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SERIE });

                ddlSubserie.DataSource = this.Subseries;
                ddlSubserie.DataBind();
                ddlSubserie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSERIE });

                ddlTipoDocumental.DataSource = this.TiposDocumentales;
                ddlTipoDocumental.DataBind();
                ddlTipoDocumental.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_DOCUMENT_TYPE });
            
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        public void LoadSubSeries()
        {   
            try
            {
                ddlSubserie.DataSource = this.Subseries;
                ddlSubserie.DataBind();
                ddlSubserie.Items.Insert(0, new ListItem() { Value = "0", Text = GlobalConstants.DDL_INITIAL_SUBSERIE });
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }

        }

        public void LoadSearch()
        {
            try
            {
                gvExpedientes.DataSource = Expedientes.ToList();
                gvExpedientes.DataBind();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion        
    }
}
